var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total_goods_sum:0,
    type:1,
    total_price:0,
    cart_list:[],
    ids:'',
    selected:false,
    userInfo:{},
    pay_amount:0
  },

  selectList(){
    if(this.data.selected==false){
      var dikou = this.data.userInfo.dikou
      if(Number(dikou)>=Number(this.data.total_price)){
        var pay_amount = 0
      }else{
        var pay_amount = Number(this.data.total_price) - Number(dikou)
      }
      pay_amount = pay_amount.toFixed(2)
      this.setData({
        selected:true,
        pay_amount:pay_amount
      })
      
    }else{
      this.setData({
        selected:false,
        pay_amount:this.data.total_price
      })
    }
  },

  checkOut(){
    var that = this;
    var phone = '';
    if(getApp().globalData.userInfo.phone){
      phone = getApp().globalData.userInfo.phone
    }
    if(phone==''||phone==null){
      wx.showModal({
        title: '提示',
        content: '请您完善个人信息后，再进行结算哦！',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/setting/addr',
            })
          }
        }
      })
      return
    }

    if(this.data.selected==true){
      var dikou = this.data.userInfo.dikou
      if(Number(dikou)>=Number(this.data.total_price)){
        var pay_amount = 0
        var score = Number(this.data.total_price)
      }else{
        var pay_amount = Number(this.data.total_price) - Number(dikou)
        var score = Number(dikou)
      }
      pay_amount = Number(pay_amount).toFixed(2)
      score = Number(score).toFixed(2)
    }else{
      var score = 0
      var pay_amount=this.data.total_price
      pay_amount = Number(pay_amount).toFixed(2)
    }

    wx.showModal({
      title: '温馨提示',
      content: '确定支付吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.httpUrl+"api/device/Payorder",
            header: {
                'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
            },
            data:{ids:that.data.ids,type:that.data.type,user_id:that.data.userInfo.id,pay_amount:pay_amount,score:score,total_price:that.data.total_price},
            method: 'POST',
            dataType: 'json',
            success: function (res) {
              app.cart_count()
              if(res.data.code==400){
                wx.showModal({
                  content: '当前余额不足请先充值！',
                })
                setTimeout(function(){
                  wx.navigateTo({
                    url: '/pages/mine/recharge',
                  })
                },2000)
              }else if(res.data.code==500){
                  wx.showModal({
                    content: res.data.msg,
                  })
              }else{
                wx.showToast({
                  title: '支付成功',
                })
                setTimeout(function(){
                  wx.navigateTo({
                    url: '/pages/order/index',
                  })
                },2000)
              }
            }
          })
        }
      }
    })

  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    

    this.setData({
      ids:options.ids
    })
    if(options.type){
      this.setData({
        type:options.type
      })
    }

    var that = this;

    wx.request({
      url: app.globalData.httpUrl+"api/mini/cart_pay",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      data:{ids:options.ids,type:that.data.type,user_id:app.globalData.userInfo.id},
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        that.setData({
          total_goods_sum:res.data.count,
          cart_list:res.data.list,
          total_price:res.data.total,
          pay_amount:res.data.total
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  //用户
  user_info:function(id){
    var that = this;
    wx.request({
      url: app.globalData.httpUrl+"api/mini/user_info",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      data:{'id':id},
      dataType: 'json',
      success: function (res) {
          app.globalData.userInfo = res.data;
          that.setData({
            userInfo:app.globalData.userInfo,
          })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.user_info(app.globalData.userInfo.id)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})